<?php

namespace app\common\model\Subject;

use think\Model;
//引入软删除
use traits\model\SoftDelete;
/**
 * 课程模型
 */
class Subject extends Model
{
    //使用软删除 
    use SoftDelete;
    protected $table = 'pre_subject';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'createtime';
    protected $updateTime = false;
    //设置软删除字段
    protected $deleteTime = 'deletetime';
    protected $append = [
        'content_text',  // 内容
        'likes_text', //点赞数量
        'thumbs_text', // 课程图片
    ];

    // 格式描述内容
    public function getContentTextAttr($value, $data)
    {
        // $content = !empty($data['content']) ? $data['content'] : '';
        return strip_tags($data['content']);
    }

    public function getLikesTextAttr($value, $data)
    {
        $likes = !empty($data['likes']) ? explode(',', $data['likes']) : [];

        return count($likes);
    }

    public function getThumbsTextAttr($value, $data)
    {
        $thumbs = !empty($data['thumbs']) ? $data['thumbs'] : '/assets/home/images/shuijiao.jpg';

        return $thumbs;
    }

    // 关联查询（根据业务需求）
    public function category()
    {
        return $this->belongsTo('app\common\model\Subject\Category', 'cateid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
